import { Context, Next } from 'koa'
import Router from 'koa-router'

export const router = new Router()

const callback = (content: { authorize: unknown }) => {
  return (ctx: Context, next: Next) => {
    const authorize = content.authorize
    if (authorize && typeof authorize === 'function') {
      return authorize.call(null, ctx, next)
    }
    return next()
  }
}

export const Get = (path: string) => {
  return (context: unknown, key: string) => {
    const controlle = context[key]
    router.get(path, callback(controlle), controlle)
  }
}

export const Post = (path: string) => {
  return (context: unknown, key: string) => {
    const controlle = context[key]
    router.post(path, callback(controlle), controlle)
  }
}
